<?php

require_once 'Handler.php';
require_once 'Db/ActiveRecord.php';

class LayeredContent_DataStore extends LayeredContent_Handler
{
	/**
	 * The database adaptor
	 * 
	 * @var Zend_Db_Adapter_Abstract
	 */
	public $db;
	
	public function init()
	{
		$this->db = Zend_Db::factory(
			$this->config->database->adapter,
			$this->config->database
		);
	}
	
	public function lookupData()
	{
		$path = $this->layeredContent->getHandler('urlHandler')->getFullPath();
		$select = $this->db->select()
					   ->from('resources', 'resources.*')
					   ->joinLeft('revisions', 'resources.id = revisions.resource_id', 'revisions.*')
					   ->joinLeft('templates', 'revisions.template_id = templates.id', 'templates.*')
					   ->where('path = ?', $path);
		$resource = $this->db->fetchAll($select);
		return $resource ? $resource : null;
	}
	
	public function getResource()
	{
		$this->lookupData();
		$path = $this->layeredContent->getHandler('urlHandler')->getFullPath();
		$select = $this->db->select()
					   ->from('resources')
					   ->where('path = ?', $path);
		$resource = $this->db->fetchRow($select);
		return $resource ? $resource : null;
	}
	
	public function getTemplate()
	{
		
	}
}

